home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14062 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: lyra.csx.cam.ac.uk!news
  2. From: gdr11@cl.cam.ac.uk (Gareth Rees)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: ANSI C and POSIX (was Re: C/C++ knocks the crap out of Ada)
  5. Date: 11 Apr 1996 15:34:27 +0100
  6. Organization: Cambridge University Computer Lab
  7. Message-ID: <yxspw9eommk.fsf@stint.cl.cam.ac.uk>
  8. References: <JSA.96Feb16135027@organon.com> <dewar.828936837@schonberg>
  9.     <4kb2j8$an0@solutions.solon.com> <dewar.829011320@schonberg>
  10.     <4kcsnsINNgkb@keats.ugrad.cs.ubc.ca> <dewar.829051685@schonberg>
  11.     <829066525snz@genesis.demon.co.uk> <dewar.829096975@schonberg>
  12. NNTP-Posting-Host: stint.cl.cam.ac.uk
  13. X-Newsreader: Gnus v5.0.15
  14.  
  15. Robert Dewar <dewar@cs.nyu.edu> wrote:
  16. > Highly imprecise thinking here I fear. There is no practical way for
  17. > any implementation to do the check you mention (this = value in 3rd
  18. > argument that is greater than the available buffer size).
  19.  
  20. But the *programmer* can do the check while writing the program!  The
  21. programmer must have declared or allocated a buffer of some size.
  22. Consider: if I wrote code like
  23.  
  24.     #include<stdio.h>
  25.  
  26.     int main () {
  27.         char buf[80];
  28.         /* Input lines are always 78 characters or less, so the limit of
  29.            100 can never cause a buffer overflow */
  30.         while (fgets(buf,100,stdin))
  31.             puts(buf);
  32.         return 0;
  33.     }
  34.  
  35. you would quite rightly point out that this is gratuitously broken and
  36. that it would cost me nothing (and gain a lot) to change `100' to `80'.
  37. It's the same with the buffer size in `read'.  Why make the byte count
  38. larger than the buffer size when you don't have to?
  39.  
  40. -- 
  41. Gareth Rees
  42.